﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing                 Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: TaskProcessingContext.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System.Diagnostics;
using System.Web.Configuration;
using Hyldahl.Services.TaskProcessing.Configuration;
using Hyldahl.Services.TaskProcessing.Data;

namespace Hyldahl.Services.TaskProcessing
{
    public class TaskProcessingContext
    {
        /*****************************************************
         * FIELDS
         *****************************************************/
        private TaskDataProvider            dataProvider;
        private TaskProcessorPluginManager  pluginManager;

        /*****************************************************
         * CONSTRUCTORS
         *****************************************************/

        /// <summary>
        /// Initializes a new instance of the <see cref="TaskProcessingContext"/> class.
        /// </summary>
        protected TaskProcessingContext() { }

        /*****************************************************
         * PROPERTIES
         *****************************************************/

        /// <summary>
        /// Gets the data store.
        /// </summary>
        /// <value>The data store.</value>
        public virtual TaskDataProvider DataStore
        {
            get
            {
                lock (this)
                {
                    if (dataProvider == null)
                    {
                        Trace.TraceInformation("Creating data provider.");
                        dataProvider = (TaskDataProvider)ProvidersHelper.InstantiateProvider(TaskProcessingServiceConfiguration.Configuration.DataProvider, typeof(TaskDataProvider));
                    }
                }

                return dataProvider;
            }
        }

        /// <summary>
        /// Gets the task processor plugins.
        /// </summary>
        /// <value>The task processor plugins.</value>
        public virtual TaskProcessorPluginManager TaskProcessorPlugins
        {
            get
            {
                lock (this)
                {
                    if (pluginManager == null)
                    {
                        Trace.TraceInformation("Creating plug-in manager.");
                        pluginManager = new TaskProcessorPluginManager(this);
                    }
                }

                return pluginManager;
            }
        }

    }
}
